import { _decorator, Component, easing, Node, Sprite, Tween, tween, Vec3 } from 'cc';
import { ResMgr } from '../Mgr/ResMgr';
import { Direction, Global, SpriteStype } from '../Global';
import { GameScene } from './GameScene';
import { CatLoad } from './CatLoad';
const { ccclass, property } = _decorator;

export type SpriteInfo = {
    type: number
}

@ccclass('GameSingleSprite')
/**
 * 游戏元素
 */
export class GameSingleSprite extends Component {

    //精灵样式
    @property(Node)
    private Style: Node = null;

    //精灵信息
    public SpriteInfo = {
        type: -1
    };

    //是否显示
    public IsShow: boolean = false;

    //精灵索引
    public SpriteIndex: number = 0;

    //是否可以消失
    public IsClear: boolean = false;

    //精灵缩放尺寸
    private ScaleNum: number = 0.7;

    /**
     * 更新精灵样式
     */
    public UpdateSpriteStyle(_info: SpriteInfo) {
        this.IsClear = false;
        this.IsShow = true;
        this.Style.active = true;
        this.SpriteInfo = _info;

        this.Style.setScale(new Vec3(this.ScaleNum, this.ScaleNum, this.ScaleNum));
        this.Style.getComponent(Sprite).spriteFrame = ResMgr.Instance.SpriteFrames[SpriteStype + _info.type];
    }

    /**
     * 初始化
     */
    public Init() {
        this.IsClear = false;
        this.IsShow = false;
        this.Style.active = false;
        this.SpriteInfo = {
            type: - 1
        }
    }

    /**
     * 播放出现动画
     */
    public PlayAppearAni() {
        Tween.stopAllByTarget(this.Style);

        this.Style.scale = new Vec3(0, 0, 0);
        tween(this.Style).to(Global.PlayDisTime + 0.1, { scale: new Vec3(this.ScaleNum, this.ScaleNum, this.ScaleNum) }).start();
    }

    /**
     * 播放滑动动画
     * @param _direction 滑动方向
     * @param _disnum 移动距离
     */
    public PlaySlideAni(_direction: string, _disnum: number) {
        let target_x: number = 0;
        let target_y: number = 0;

        switch (_direction) {
            case Direction.UP:
                target_y += _disnum * Global.BrickDis;
                break;

            case Direction.DOWN:
                target_y -= _disnum * Global.BrickDis;
                break;

            case Direction.LEFT:
                target_x += _disnum * Global.BrickDis;
                break;

            case Direction.RIGHT:
                target_x -= _disnum * Global.BrickDis;
                break;
        }

        Tween.stopAllByTarget(this.Style);
        this.Style.setPosition(new Vec3(target_x, target_y));
        tween(this.Style).to(Global.SlideTime, { position: new Vec3(0, 0) }).start();
    }

    /**
     * 播放提示消失动画
     */
    public PlayCanClearAni() {
        if (!this.IsClear) return;

        this.Style.setScale(new Vec3(this.ScaleNum, this.ScaleNum, this.ScaleNum));
        tween(this.Style).delay(Global.PlayDisTime * 2).to(Global.PlayDisTime, { scale: new Vec3(this.ScaleNum - 0.5, this.ScaleNum - 0.5, this.ScaleNum - 0.5) }, { easing: easing.backIn }).call(() => {
            this.Init();
        }).start();

        GameScene.Instance.CatLoad.getComponent(CatLoad).UpdateCatFruitNum(1, this.SpriteInfo.type);
    }


}


